package dao;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.Query;

import org.junit.Test;

import util.Date_String;
import util.EntityManagerUtil;
import entity.Book;
import entity.BookPK;
import entity.Customer;
import entity.Room;
import entity.User;

public class BookDao {

	/*
	 * 能够根据标准和日期查询客房空闲情况；能够为指定客户预订指定的客房，
	 * 并记录预订开始时间、预订结束时间、联系人、联系电话、前台操作员、操作时间等信息。
	 * */
	public static List<Room> roomAdanvce(String standard,String btime,String etime){
		EntityManager em = EntityManagerUtil.getEntityManager();
		String sql = null;
		if(standard.equals("") || standard == null){
			sql = "SELECT * FROM room r WHERE r.rno not in (SELECT rno from advance a WHERE a.btime between '"+btime+"' and  '"+etime+"' UNION SELECT rno from book b WHERE b.btime between '"+btime+"' and  '"+etime+"')";
		}else{ sql = "SELECT * FROM room r WHERE r.rno not in (SELECT rno from advance a WHERE a.btime between '"+btime+"' and  '"+etime+"' UNION SELECT rno from book b WHERE b.btime between '"+btime+"' and  '"+etime+"') and r.standard='"+standard+"'";}
		Query query=em.createNativeQuery(sql, Room.class);
		List<Room> Rooms = query.getResultList();
		em.close();
		return Rooms;
	}

	public static List<Map<String,String>> selectList(String standard, String btime, String etime){
		List<Map<String,String>> tt = new ArrayList<Map<String,String>>();

		List<Room> Rooms = roomAdanvce(standard, btime, etime);

		Map<String,String> cc = null;
		for (Room user: Rooms) {
			cc = new HashMap<String,String>();
			cc.put("rno", user.getRno());
			cc.put("fno", user.getFno());
			cc.put("standard", user.getStandard());
			cc.put("cost", String.valueOf(user.getCost()));
			cc.put("position", user.getPosition());
			tt.add(cc);
		}
		return tt;
	}
	public static Book getBook(String rno,String cid,String btime){
		EntityManager em=EntityManagerUtil.getEntityManager();
		BookPK Bookpk=new BookPK();
		Bookpk.setRno(rno);
		Bookpk.setCid(cid);
		Bookpk.setBtime(Date_String.setDateYmd(btime));
		Book Book = null;
		try{
			Book =em.find(Book.class, Bookpk);
		}catch(NoResultException e){
			Book = null;
		}
		em.close();
		return Book;
	}
	public static void addBook(String rno,String cid,String btime,int userid,String etime,double prepay) throws Exception{
    	EntityManager em=EntityManagerUtil.getEntityManager();
		if(getBook(rno,cid,btime)!=null){
			throw new Exception("房间号为<"+rno+">已被入住!");
		}
		BookPK Bookpk=new BookPK();
		Bookpk.setRno(rno);
		Bookpk.setCid(cid);
		Bookpk.setBtime(Date_String.setDateYmd(btime));
		Book Book=new Book();
		Book.setId(Bookpk);
		em.getTransaction().begin();
		Room room=em.find(Room.class, rno);
		User user=em.find(User.class, userid);
		Customer customer = em.find(Customer.class, cid);
		Book.setRoom(room);
		Book.setUser(user);
		Book.setCustomer(customer);
		Book.setEtime(Date_String.setDateYmd(etime));
		Book.setPrepay(BigDecimal.valueOf(prepay));
		em.persist(Book);
		em.refresh(room);
		em.refresh(customer);
		em.getTransaction().commit();
		em.close();
    }
	
	@Test
	public void test(){ 
		  try {
			BookDao.addBook("r007", "430523199605054338", "2018-01-11", 1, "2018-1-12", 100.0);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
